package com.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.eclipse.jdt.internal.compiler.batch.Main;

public class MD5 {
    public static String getMD5(String plainText) {
    	int length=32;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
				//获取MD5实例
            md.update(plainText.getBytes());//传入要加密的byte类型值
            byte[] digest = md.digest();//md5加密后的byte类型值

            /*
               自定义二次加密：   
            */
            int i;
            StringBuilder sb = new StringBuilder();
            for (int offset = 0; offset < digest.length; offset++) {
                i = digest[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    sb.append(0);
                sb.append(Integer.toHexString(i));//通过				  						Integer.toHexString方法把值变为16进制
            }
            return sb.toString().substring(2, length);
				//从下标0开始，length目的是截取多少长度的值
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) {
    	MD5 md5 = new MD5();
    	System.out.println(md5.getMD5("123456"));
    			
    }
}